package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车模块业务类
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addToShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 1. 判断是套餐还是菜品--->决定要查询套餐表还是菜品表--->获取基本信息（图片、价格、名称）
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 设置菜品口味
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());

        if (shoppingCartDTO.getSetmealId() != null) {
            // 此时添加的是套餐
            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());

            // 补充套餐的基本信息
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setSetmealId(setmeal.getId());
        } else if (shoppingCartDTO.getDishId() != null) {
            // 添加的是菜品
            Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());

            // 补充菜品的基本信息
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setDishId(dish.getId());
        } else {
            throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_ID_IS_NULL);
        }
        // 2. 查询购物车中是否已经存在当前添加的项
        ShoppingCart shoppingCartItem = shoppingCartMapper.getByShoppingCart(shoppingCart);

        if (shoppingCartItem != null) {
            // 2.1 如果存在，将数量+1，然后update回表
            shoppingCartItem.setNumber(shoppingCartItem.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCartItem);
        } else {
            // 2.2 如果不存在，将数量=1，然后insert到表中
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.save(shoppingCart);
        }


    }

    /**
     * 查看当前用户购物车信息
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.getByUserId(userId);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
